package com.senpai.ikhtiar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.senpai.database.Answer;
import com.senpai.database.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class SplashScreen extends Activity implements AnimationListener {

	int number_question = 0;
	String myJsonPartis;
	TextView id_q, category_q, question_q;
	String Category;
	Animation animFadein;
	ImageView imgView;

	int idQuestion, answer;
	String parti, question, formattedDate;

	DatabaseHelper database = new DatabaseHelper(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		imgView = (ImageView) findViewById(R.id.logo_ikhtiar);
		getPartis();
		Timer t = new Timer();
		t.schedule(new splash(), 3000);

		animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.fade_in);

		animFadein.setAnimationListener(this);
		imgView.setVisibility(View.VISIBLE);
		imgView.startAnimation(animFadein);
	}

	private void getPartis() {

		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"Partis.json")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		myJsonPartis = sb.toString();

		try {

			JSONObject jsonObjMain = new JSONObject(myJsonPartis);
			JSONArray jsonArray = jsonObjMain.getJSONArray("Partis");

			for (int i = 0; i < jsonArray.length(); i++) {

				JSONObject jsonObj = jsonArray.getJSONObject(i);

				idQuestion = jsonObj.getInt("Id");
				parti = jsonObj.getString("Name_Parti");
				answer = jsonObj.getInt("Id_Answer");

				database.addAnswer(new Answer(idQuestion, parti, Integer
						.toString(answer)));

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.splash_screen, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class splash extends TimerTask {

		@Override
		public void run() {
			Intent i = new Intent(SplashScreen.this, MainActivity.class);
			finish();
			startActivity(i);
		}

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

}
